<?php

class IndexController extends WapBaseC {

    public $max_num = 520; //手机端最大显示数量
    public $page_size = 40; //每页的个数
    public $cache_array = array(//不同类别对应的缓存key
        'index' => 'getWapHomeGoods',
        'jj' => 'getWapJJGoods',
        'jd' => 'getWapJdGoods',
        'tmall' => 'getWapTmallGoods',
        'qd' => 'getWapQiuDongGoods',
        'cat_1' => 'getWapcat_1Goods',
        'cat_2' => 'getWapcat_2Goods',
        'cat_3' => 'getWapcat_3Goods',
        'cat_4' => 'getWapcat_4Goods',
        'cat_5' => 'getWapcat_5Goods',
        'cat_6' => 'getWapcat_6Goods',
        'cat_7' => 'getWapcat_7Goods',
        'cat_8' => 'getWapcat_8Goods',
        'cat_9' => 'getWapcat_9Goods',
    );

    /**
     * 首页商品展示
     * author: SongXing
     */
    public function actionIndex() {
        $type = 'index';
        $GoodsModel = new WapGoodsModel();
        $all_goods = $GoodsModel->getWapHomeGoods($this->max_num, $this->cache_array[$type]); //获取所有的商品
        $goods = array_slice($all_goods[0], 0, $this->page_size); //第一次加载page_size个
        $this->render('index', array(
            'goods' => $goods,
            'page' => 1,
            'type' => $type,
        ));
    }

    /**
     * 9.9专区展示
     * author: SongXing
     */
    public function actionJj() {
        SeoWord::setWord($this, 'getCatSeo', 'jkj');
        $type = 'jj';
        $GoodsModel = new WapGoodsModel();
        $all_goods = $GoodsModel->getJJGoods($this->max_num, $this->cache_array[$type]);  //获取所有的商品
        $goods = array_slice($all_goods[0], 0, $this->page_size); //第一次加载page_size个
        $this->render('index', array(
            'goods' => $goods,
            'page' => 1,
            'type' => $type,
        ));
    }

    /**
     * 京东专区展示
     * author: SongXing
     */
    public function actionJd() {
        SeoWord::setWord($this, 'getCatSeo', 'jd');
        $type = 'jd';
        $GoodsModel = new WapGoodsModel();
        $all_goods = $GoodsModel->getJdGoods($this->max_num, $this->cache_array[$type]);  //获取所有的商品
        $goods = array_slice($all_goods[0], 0, $this->page_size); //第一次加载page_size个
        $this->render('index', array(
            'goods' => $goods,
            'page' => 1,
            'type' => $type,
        ));
    }

    /**
     * 天猫专区展示
     * author: SongXing
     */
    public function actionTmall() {
        $type = 'tmall';
        $GoodsModel = new WapGoodsModel();
        $all_goods = $GoodsModel->getTmallGoods($this->max_num, $this->cache_array[$type]);  //获取所有的商品
        $goods = array_slice($all_goods[0], 0, $this->page_size); //第一次加载page_size个
        $this->render('index', array(
            'goods' => $goods,
            'page' => 1,
            'type' => $type,
        ));
    }

    /**
     * 秋冬专区展示
     * author: SongXing
     */
    public function actionQiuDong() {
        $type = 'qd';
        $GoodsModel = new WapGoodsModel();
        $all_goods = $GoodsModel->getRecommendGoods($this->max_num, $this->cache_array[$type]);  //获取所有的商品
        $goods = array_slice($all_goods[0], 0, $this->page_size); //第一次加载page_size个
        $this->render('index', array(
            'goods' => $goods,
            'page' => 1,
            'type' => $type,
        ));
    }

    /**
     * 栏目展示
     * author: SongXing
     */
    public function actionCat() {
        $cid = (int) !empty($_GET['cid']) ? $_GET['cid'] : 0;
        $type = 'cat_' . $cid;
        $GoodsModel = new WapGoodsModel();
        $all_goods = $GoodsModel->getCatGoods($this->max_num, $this->cache_array[$type]);  //获取所有的商品
        $goods = array_slice($all_goods[0], 0, $this->page_size); //第一次加载page_size个
        $this->render('index', array(
            'goods' => $goods,
            'page' => 1,
            'type' => $type,
            'cid' => $cid,
        ));
    }

    /**
     * 商品详情页面  (一般是直接跳转)
     * author: SongXing
     */
    public function actionShow() {
        $good_id = (int) $_GET['gid'];
        $GoodsModel = new GoodsModel();
        $good = $GoodsModel->getGoodBYGoodId($good_id);
        if (!empty($good)) {
            if (empty($good['click_url'])) {
                $this->redirect($good['detail_url']);
            } else {
                $this->redirect($good['click_url']);
            }
            return true;
        }
        $this->render('noGoodShow');
    }

    /**
     * ajax 根据ID获取商品
     */
    public function actionAjaxGoods() {
        $type = !empty($_GET['type']) ? $_GET['type'] : 'index'; //第几页
        $page = (int) $_GET['page'] > 1 ? $_GET['page'] : 1; //第几页
        $page_size = (int) $this->page_size; //每页的个数
        $ids = Yii::app()->cache->get($this->cache_array[$type]);
        //var_dump($this->cache_array[$type]);
        if (!empty($ids[0])) {
            $ids = $ids[0];
            $ids_get = array_slice($ids, $page * $page_size, $page_size);   //取前面的 N个元素
        } else {
            return '';
        }
        foreach ($ids_get as $good) {
            include $this->getViewFile('../lib/goodlist');
        }
    }

}
